<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 *
 *
 * @package default
 * @author kxgsy163@163.com
 */


class whyshopseckill_ctl_admin_whyshopseckill extends desktop_controller{

    //供应商中的商品id 用于生成html、
    private $_filter_goods = null;

    var $workground = 'whyshopseckill_ctl_admin_whyshopseckill';




    function __construct($app) {
        parent::__construct($app);
        $this->_request = kernel::single('base_component_request');
    }

    function index(){

       
 $custom_actions[]= array('label'=>app::get('whyshopseckill')->_('添加秒杀商品'),'icon'=>'add.gif','href'=>'index.php?app=whyshopseckill&ctl=admin_whyshopseckill&act=add','target'=>'_blank');


        $actions_base['title'] = app::get('b2c')->_('添加秒杀商品');
        $actions_base['actions'] = $custom_actions;
        $actions_base['allow_detail_popup'] = true;//详情弹出
        $actions_base['use_buildin_set_tag'] = true;//是否支持设置标签
        $actions_base['use_buildin_export'] = true;//导出
        $actions_base['use_buildin_filter'] = true;//是否支持高级筛选
        $actions_base['use_view_tab'] = true;//是否支持使用tab

        $this->finder('whyshopseckill_mdl_whyshopseckill',$actions_base
            );
    }

    public function add() {
        
       // $this->display('admin/whyshopseckill/edit_pop.html');

         $this->singlepage('admin/whyshopseckill/edit.html');
    }

    /**
     * 修改供应商
     **/
    public function edit() {
        $id = $this->_request->get_get('whyshopseckill_id');
        $arr = $this->app->model('whyshopseckill')->dump($id);
       
        $this->pagedata['whyshopseckill'] = $arr;

        $filter['whyshopseckill_id']=$id;
        $arr_goods = $this->app->model('whyshopseckill_goods')->getList('*',$filter,0,-1);//取当前秒杀活动下的所有秒杀商品信息


        foreach ($arr_goods as $key => $value) {
            # code...
            $goods_id[]=$value['seckill_goods_id'];//返回给value用。纯商品ID数组

            $new_rs_goods[$value['seckill_goods_id']]=$value;//这个是返回给rel_item_seckill用，完整的秒杀商品数组，以商品id为键名
        }
       // print_r($goods_id);exit;
        $this->pagedata['whyshopseckill_goods_id'] = $goods_id;

        $this->pagedata['whyshopseckill_goods'] = $new_rs_goods;

       // $this->_edit();
        $this->singlepage('admin/whyshopseckill/edit.html');
    }


    /*
     * 模板
     */
    private function _edit()
    {
       /* $this->pagedata['giftnum'] = range(1,12);
        //////////////////////////// 会员等级 //////////////////////////////
        $mMemberLevel = app::get('b2c')->model('member_lv');
        $this->pagedata['member_level'] = $mMemberLevel->getList('member_lv_id,name', array(), 0, -1, 'member_lv_id ASC');

        $this->pagedata['sections'] = array(
            array('file'=>'admin/basic.html','label'=>'基本设置'),
            array('file'=>'admin/goods.html','label'=>'商品设置'),
            array('file'=>'admin/intro.html','label'=>'礼包设置'),
        );

        #$this->giftpackage
        $this->pagedata['html'] = $this->get_html(true);

         //是否显示积分
        if( app::get('b2c')->getConf('site.get_policy.method')==1 ) $this->pagedata['show_score'] = 'false';*/
    }
    #End Func
    /**
     * 保存供应商
     **/
    public function toAdd() {
        $data =$this->_request->get_post();
    
     $full_start_time=$data['seckill_starttime']." ".$data['_DTIME_']['H']['seckill_starttime'].":".$data['_DTIME_']['M']['seckill_starttime'];

          $full_end_time=$data['seckill_endtime']." ".$data['_DTIME_']['H']['seckill_endtime'].":".$data['_DTIME_']['M']['seckill_endtime'];

// print_r($full_start_time);exit;

        $data['seckill_starttime'] =strtotime($full_start_time);
        $data['seckill_endtime'] =strtotime($full_end_time);
        $o = $this->app->model('whyshopseckill');

                $o_goods = $this->app->model('whyshopseckill_goods');

        $this->begin();

       // $data['add_time']=time();
        //print_r($data);exit;
        if($o->save($data) ){
            $whyshopseckill_id=$data['whyshopseckill_id'];// 

            if($whyshopseckill_id){
                //插入商品goods数组
                $filter['whyshopseckill_id']=$whyshopseckill_id;
                 $o_goods->delete($filter);
                //$data['goods']
                foreach ($data['goods'] as $key => $value) {
                    # code...
                    $data_goods=$value;
                    $data_goods['whyshopseckill_id']=$whyshopseckill_id;
                    
                    $o_goods->save($data_goods);
                    
                }

            }
             $this->end( true,'添加成功！'  );
            
        }
           
        else{
            $this->end( false,'添加失败！'  );
        }
    }

}
